// curriculum_package/pages/studentsDetail/studentsDetail.ts
// 获取学员信息列表
import {userInfoList} from '../../../api/member'
Component({
  data:{
    studentDetailList:[
      {
        title:"姓名",
        props:"userName"
      },{
        title:"昵称",
        props:"nickName"
      },
      {
        title:"性别",
        props:"sex"
      },
      {
        title:"关系",
        props:"familyRel"
      },
      {
      title:"身份证号",
      props:"idCard"
      },
      {
        title:"生日",
        props:"birthday"
      },{
        title:"联系方式",
        props:"phonenumber"
      },{
        title:"校区",
        props:"schoolId"
      }
    ],
    studentList:[],
    currentIndex:0,
    avatar:"",
    show:false,
  },
  methods:{
  onShow(){
    this.getUserinfoList()
  },
  arrow(e:any){
    const {type} = e.currentTarget.dataset
     let currentIndex = this.data.currentIndex
     const len = this.data.studentList.length
     switch(type){
       case 'left':
        if(currentIndex > 0){
          currentIndex--
         }
        break;
        case 'right':
          if(currentIndex < len - 1){
            currentIndex++
           }
        break;
     }
    this.setData({
      currentIndex
    })
  },
    // 获取用户信息列表
  async getUserinfoList(){
   const userId = getApp().getUserInfo().userId
   const {code,data:res} = await  userInfoList({
      userId
     })
    if(code === 200){
      let infoList = res as any
      if(infoList.length === 0) {
        this.setData({
          show:true
        })
      }
     const newList = infoList.reduce((current:any,val:any)=>{
        val.id
       let childList = this.data.studentDetailList.map(item=>{
         return {
           props:item.props,
           value:this.formatInfo(item.props,val[item.props])
         }
       })
       let courseList = val.seriesList
      current.push({
        id:val.id,
        avatar:val.avatar,
        childList,
        courseList
      })
      return current
      },[] as Array<any>)
      this.setData({
        studentList:newList
      })
    }
   },
  //  格式化年龄、家庭关系
   formatInfo(props:string,values:any){
     const schoolList = getApp().getSchoolList()
     if(props === "sex") return values === '0' ? '男' :'女'
     if(props === 'familyRel') return values === '0' ? '父亲' : values === '1' ? '母亲' :'祖母'
     if(props === 'schoolId') {
      return schoolList.filter((val:any)=>{
        return val.id ==  values
      })[0]?.name || ""

     }
     if(props === 'birthday') {
       const dateFormat = new Date(values)
       const year = dateFormat.getFullYear()
       const month = dateFormat.getMonth() + 1 < 10 ? '0'+ (dateFormat.getMonth() + 1) : dateFormat.getMonth()
       const day = dateFormat.getDate() < 10 ? '0' + dateFormat.getDate() : dateFormat.getDate()
        return  `${year}-${month}-${day}`
     }
     else{
       return values ? values : ''
     }
  },
  // 跳转课程详情
    skipCourseDetail(e:any){
      const {coursename,stuid,courseserialid} = e.currentTarget.dataset     
      wx.navigateTo({
        url:`/myCourse_package/pages/CourseIntroduction/CourseIntroduction?title=${coursename}&stuId=${stuid}&serialId=${courseserialid}`
      })
     },
    //修改用户信息跳转
    alterInfo(){
      const schoolList = getApp().getSchoolList()
      const studentList = this.data.studentList as any
      const item = studentList[this.data.currentIndex].childList
      let newListStr = JSON.parse(JSON.stringify(item))
      newListStr = newListStr.map((val:any)=>{
        if(val.props === 'sex'){
          val.value = val.value === "男" ? '0' :val.value === '女' ? '1' : ''
        }
        if(val.props === 'familyRel'){
          val.value = val.value === '父亲' ? 0 : val.value === '母亲' ? 1 : val.value === '姥姥'?'2':''
        }
        if(val.props === 'schoolId'){
          val.value =  schoolList.filter((item:any)=>{
            return item.name === val.value
          })[0]?.id || 0
        }
        return val
      })
      if(studentList[this.data.currentIndex].avatar){
        newListStr.push({
          props:"avatar",
          value:studentList[this.data.currentIndex].avatar
        })
      }
      const studentId = studentList[this.data.currentIndex].id
      const sendData = JSON.stringify(newListStr) 
      
      wx.navigateTo({
        url:`/curriculum_package/pages/addStudents/addStudents?childList=${encodeURIComponent(sendData)}&studentId=${studentId}`
      })
    },
    // 添加学员
add(){
  wx.navigateTo({
    url:"/curriculum_package/pages/addStudents/addStudents"
  })
},
cancel(){
  wx.navigateBack()
}
  }
})